/** Table Model based on JDBC  2.0 :

    An JTable model is defined for a SQL statement. This table model used some
features coming with JDBC 2.0 driver.

*/

import java.sql.*;
import java.util.*;
import javax.swing.table.*;
import java.rmi.*;

class GradebookClientModel extends AbstractTableModel {

    GradebookClientModel(String hostAndServerName ) {
	try {

		dbSrvObj = (GradebookServer) Naming.lookup(hostAndServerName);
		System.out.printf("Query to execute: %s\n", sql + condition + "2345");
		resultSet = dbSrvObj.getResultSet(sql + condition + "2345" + orderBy);
		getColumnTitles();
		currentRow = 2;
		recordCount = resultSet.size() - 2;
   	} catch (RemoteException re) { re.printStackTrace(); }
	  catch ( Exception ee) { ee.printStackTrace(); }
    }

    void getColumnTitles() {
	columnCount = ((DBRow) resultSet.elementAt(0)).getColumnCount();
	columnTitles = new Vector(columnCount);
	for ( int i = 0; i < columnCount; i ++ ) 
		columnTitles.add(ScreenIO.expandString( ((DBRow) resultSet.elementAt(1)).getColumn(i), 15));
    }

    public boolean isCellEditable(int row, int col) { return true; }
    public int getRowCount()    { return recordCount; }
    public int getColumnCount() { return columnCount;}
    public String getColumnName(int col) { return (String) columnTitles.elementAt(col); }
	
    public Object getValueAt(int row, int col) {
	String s = ((DBRow) resultSet.elementAt(row+2)).getColumn(col);
	currentRow = row;
	return s;
    }
    public void  setValueAt( Object obj, int row, int col ) {
    }

    public void addRow( Object obj ) {
    }

    public Object removeRow( int row ) {
	return "Hello";
    }

    public void requry(String cid ) {
	System.out.println( "Query to execute: " + sql + condition + cid + orderBy );
	try { resultSet = dbSrvObj.getResultSet( sql + condition + cid + orderBy);
	} catch ( RemoteException e ) { e.printStackTrace() ; }
	fireTableDataChanged();
	currentRow = 0;
	recordCount = resultSet.size() - 2;
    }

    // Data members
    GradebookServer	dbSrvObj = null;
    String		sql        = "SELECT CID, SID, LastName, FirstName, HW1, HW2, HW3, HW4, HW5, Midterm1, Final, Total, Grade from VGrades ";
    String 		condition  = " WHERE CID = ";
    String		orderBy    = " ORDER By LastName, FirstName ";
    Vector		resultSet;
    Vector		columnTitles;
    int currentRow = 0, columnCount = 0, recordCount = 0;
}

